package com.wynne.ptldemo.service.serviceimpl.dispather;

import com.wynne.ptldemo.bo.ActionModelBo;
import com.wynne.ptldemo.bo.TagSignalBo;
import com.wynne.ptldemo.consts.ResultBean;
import com.wynne.ptldemo.pojo.PtlActionModule;
import com.wynne.ptldemo.pojo.PtlActionModuleGroup;
import com.wynne.ptldemo.repository.PtlActionModuleGroupRepository;
import com.wynne.ptldemo.repository.PtlActionModuleRepository;
import com.wynne.ptldemo.service.IModuleService;
import com.wynne.ptldemo.service.IRouteService;
import com.wynne.ptldemo.util.SpringContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ModuleDispatcher implements IRouteService {

    @Autowired
    private PtlActionModuleGroupRepository actionModuleGroupRepository;

    @Autowired
    private PtlActionModuleRepository actionModuleRepository;

    @Override
    public ResultBean execRoute(TagSignalBo signalBo, Long amgId) {
        List<ActionModelBo> actionModelBos = findActionModules(amgId);
        return this.doModule(signalBo, actionModelBos);
    }

    private ResultBean doModule(TagSignalBo tagSignalBo, List<ActionModelBo> actionModelBos) {
        Optional<ActionModelBo> actionModelBo = getFirstActionModel(actionModelBos);
        if (!actionModelBo.isPresent()) {
            return ResultBean.createBySuccess();
        }
        IModuleService moduleService = (IModuleService) SpringContextUtil.getBean(actionModelBo.get().getCallClass());
        ResultBean resultBean = moduleService.execModule(tagSignalBo);

        if (resultBean.isSuccess()) {
            actionModelBo.get().setIsComplete(true);
            this.doModule(tagSignalBo, actionModelBos);
        }

        return resultBean;
    }

    public Optional<ActionModelBo> getFirstActionModel(List<ActionModelBo> actionModelBos) {
        return actionModelBos.stream().filter(d -> !d.getIsComplete()).findFirst();
    }

    //直接联合查询就行了
    public List<ActionModelBo> findActionModules(Long amgId) {
        List<PtlActionModuleGroup> actionModuleGroups = actionModuleGroupRepository.findByAmgIdOrderBySeq(amgId);
        List<ActionModelBo> actionModelBos = new ArrayList<>();
        actionModuleGroups.forEach(d -> {
            ActionModelBo actionModelBo = new ActionModelBo();
            actionModelBo.setAmCode(d.getAmCode());
            actionModelBo.setSeq(d.getSeq());
            actionModelBo.setIsComplete(false);
            actionModelBos.add(actionModelBo);
        });
        List<String> amCodes = actionModelBos.stream().map(d -> d.getAmCode()).collect(Collectors.toList());
        List<PtlActionModule> actionModules = actionModuleRepository.findByAmCodeIn(amCodes);

        actionModules.forEach(d -> actionModelBos.stream().
                filter(c -> d.getAmCode().equals(c.getAmCode())).
                forEach(c -> c.setCallClass(d.getCallClass())));

        return actionModelBos;
    }
}
